package ModelLayer;
import java.util.ArrayList;
/**
 * Write a description of class DVD here.
 * 
 * @author (Jan Skrubbeltrang) 
 * @version (a version number or a date)
 */
public class DVD
{
    // instance variables
    private int id;
    private String title;
    private String artist;
    private String releaseDate;
    private ArrayList<Copy> copies;
      /**
   * Initialise the instance variables for the constructor below.
   * @param none.
   * @return none.
   */
    public DVD()
    {
        // initialise instance variables
        copies = new ArrayList<Copy>();
        id = 0;
        title = "";
        artist = "";
        releaseDate = "";
    }
   
        /**
   * Initialise the instance variables for the constructor below.
   * @param title holds the title of the DVD.
   * @param artist holds the artist of the DVD.
   * @param releaseDate holds the releaseDate of the DVD.
   * @return none.
   */
    public DVD(String title, String artist, String releaseDate)
    {
        // initialise instance variables
        copies = new ArrayList<Copy>();
        this.id = 0;
        this.title = title;
        this.artist = artist;
        this.releaseDate = releaseDate;
    }

         /**
   * Initialise the instance variables for the constructor below.
   * @param title - holds the title of the DVD.
   * @param artist - holds the artist of the DVD.
   * @param releaseDate - holds the releaseDate of the DVD.
   * @param id - holds the ID of the DVD. 
   * @return none.
   */
    public DVD(int id, String title, String artist, String releaseDate)
    {
        // initialise instance variables
        copies = new ArrayList<Copy>();
        this.id = id;
        this.title = title;
        this.artist = artist;
        this.releaseDate = releaseDate;
    }
    
     /**
   * Gets the ID of the DVD.
   * @param none.
   * @return The ID of the DVD.
   */
    public int getId()
    {
        return id;
    }
    
      /**
   * Gets the title of the DVD.
   * @param none.
   * @return The title of the DVD.
   */
    public String getTitle()
    {
        return title;
    }
    
       /**
   * Gets the artist of the DVD.
   * @param none.
   * @return The artist of the DVD.
   */
    public String getArtist()
    {
        return artist;
    }
    
        /**
   * Gets the releaseDate of the DVD.
   * @param none.
   * @return The releaseDate of the DVD.
   */
    public String getReleaseDate()
    {
        return releaseDate;
    }
    
        /**
   * Sets or changes an ID of the DVD.
   * @param newID - holds the new ID of the DVD.
   * @return none.
   */
    public void setId(int newId)
    {
        id = newId;
    }
    
        /**
   * Sets or changes the title of the DVD.
   * @param newTitle - holds the new title of the DVD.
   * @return none.
   */
    public void setTitle(String newTitle)
    {
        title = newTitle;
    }

         /**
   * Sets or changes the artist of the DVD.
   * @param newArtist - holds the new artist of the DVD.
   * @return none.
   */
    public void setArtist(String newArtist)
    {
        artist = newArtist;
    }
    
         /**
   * Sets or changes the releaseDate of the DVD.
   * @param newReleaseDate - holds the new releasedate of the DVD.
   * @return none.
   */
    public void setReleaseDate(String newReleaseDate)
    {
        releaseDate = newReleaseDate;
    }
    
         /**
   * Creates a copy of a DVD, and adds it to the ArrayList<Copy>. Furthermore it gets the highest serialnumber of the other copies, and adds 1 - So that this
   * copy gets an unique serialnumber. 
   * @param dateOfPurchase - holds the date of purchase for the copy.
   * @param price - holds the price for the copy.
   * @return none.
   */
    public void createCopy(String dateOfPurchase, double price)
    {
        Copy p = new Copy(this, getHigestSerialFromCopies()+1, dateOfPurchase, price);
        copies.add(p);
    }
    
         /**
   * Gets the highest serialnumber from the copy ArrayList.
   * @param none.
   * @return the highest serialnumber.
   */
    private int getHigestSerialFromCopies()
    {
        int high = 0; 
        for(Copy c : copies)
        {
            if(c.getSerial() > high)
            {
                high = c.getSerial();
            }
        }
        return high;
    }
   
         /**
   * Gets a copy from its serialnumber.
   * @param serial - holds the serialnumber of the Copy.
   * @return the copy from the ArrayList, based on the specified serialnumber in serial.
   * @return null.
   */
    public Copy getCopy(int serial)
    {
        int index = 0;
        boolean found = false;
        while(!found && index < copies.size())
        {
            Copy c = copies.get(index);
            if(c.getSerial() == serial)
            {
                found = true;
            }
            else 
            {
                index++;
            }
        }
        if(found)
        {
            return copies.get(index);
        }
        else
        {
            return null;
        }
    }
    
          /**
   * Removes a copy with the specified number in serial, from the ArrayList.
   * @param serial - holds the serialnumber of the Copy.
   * @return none.
   */
    public void removeCopy(int serial)
    {
        int index = 0;
        boolean found = false;
        while(!found && index < copies.size())
        {
            Copy c = copies.get(index);
            if(c.getSerial() == serial)
            {
                found = true;
            }
            else 
            {
                index++;
            }
        }
        if(found)
        {
            copies.remove(index);
        }
    }
    
           /**
   * Sets the object newCopy of the class Copy.
   * @param newCopy - holds the new object Copy of the class Copy.
   * @return none.
   */
    public void setCopy(Copy newCopy)
    {
        int index = 0;
        boolean found = false;
        while(!found && index < copies.size())
        {
            Copy c = copies.get(index);
            if(c.getSerial() == newCopy.getSerial())
            {
                found = true;
            }
            else 
            {
                index++;
            }
        }
        if (found)
        {
            copies.set(index, newCopy);
        }
    }
    
             /**
   * Lists all the copies from the ArrayList<Copy>.
   * @param none.
   * @return all the copies from the ArrayList<Copy.
   */
    public ArrayList<Copy> listAllCopies()
    {
        return copies;
    }
}


















